/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.sigemfi.vnf.converter;

import edu.upn.sigemfi.ope.be.Moneda;
import edu.upn.sigemfi.seg.be.Agencia;
import edu.upn.sigemfi.vnf.beans.AgenciaController;
import edu.upn.sigemfi.vnf.beans.MonedaController;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/**
 *
 * @author Administrador
 */
public class AgenciaConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {
        if (arg2 == null) {
            return null;
        }
        try {
            Long id = Long.parseLong(arg2);

            AgenciaController ac = (AgenciaController) FacesContext.getCurrentInstance().getApplication().getVariableResolver().resolveVariable(FacesContext.getCurrentInstance(), "AgenciaController");
            return ac.buscar(id);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {
        if (arg2 == null) {
            return null;
        }
        if (arg2 instanceof Agencia) {
            Agencia a = (Agencia) arg2;
            return a.getIdAgencia().toString();
        } else {
            throw new IllegalArgumentException("El objeto que se ha pasado no es de tipo moneda");
        }
    }
}
